/**
 * 
 */
package org.compiere.process;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.MBPMForm;
import org.compiere.model.MBPMFormCell;
import org.compiere.model.MBPMFormColumn;
import org.compiere.model.MBPMFormLine;
import org.compiere.model.MFormula;
import org.compiere.model.MParameter;
import org.compiere.model.X_BSC_Parameter;
import org.compiere.util.Msg;

/**
 * @author V.Sokolov
 *
 */
public class BPM_RefreshFormStructure extends SvrProcess {

	MBPMForm form = null;
	MBPMFormLine[] formLine = null;
	MBPMFormColumn[] listColumn = null;
	
	/* 
	 */
	@Override
	protected void prepare() {
		
		form = new MBPMForm(getCtx(), getRecord_ID(), get_TrxName());
		listColumn = MBPMFormColumn.getLineFormCode(getCtx(), form.getBPM_FormCode_ID(), get_TrxName());
		formLine = MBPMForm.getLines(getCtx(), form.getBPM_Form_ID(), get_TrxName());
		
		if(listColumn.length == 0)
			throw new AdempiereException("Отсутствуют колонки для [ "+form.getBPM_FormCode().getName()+" ]");
	}

	/* 
	 */
	@Override
	protected String doIt() throws Exception {
		
		for(MBPMFormLine line: formLine){

			for(MBPMFormColumn column: listColumn){
			
				MBPMFormCell ce = MBPMFormCell.getCell(getCtx(), column.getBPM_FormColumn_ID(), line.getBPM_FormLine_ID(), get_TrxName());
				if(ce == null){
					MBPMFormCell cell = new MBPMFormCell(getCtx(), 0, get_TrxName());
					cell.setBPM_FormColumn_ID(column.getBPM_FormColumn_ID());
					cell.setBPM_FormLine_ID(line.getBPM_FormLine_ID());
					// create Parameter
					MParameter param = MParameter.createParameter(form.getBPM_FormCode().getName()+" - L{"+line.getName()+"} C{"+column.getName()+"}", MFormula.getFormulaValue_ID(), X_BSC_Parameter.MODULES_BPM);
					cell.setBSC_Parameter_ID(param.getBSC_Parameter_ID());
					cell.setAlignment(column.getAlignment());
					cell.setVerticalAlignment(column.getVerticalAlignment());
					cell.setisBold(line.isBold());
					cell.setBPM_DataType(column.getBPM_DataType());
					cell.saveEx();
				}
			}
		}
		
		return Msg.translate(getCtx(), "Success");
	}

}
